package ClassLoader;

import FileIO.BinaryFileUtils;

import java.io.IOException;

/**
 * @Author shangchao
 * @Date 2020/9/26 16:09
 */
public class MyClassLoader extends ClassLoader{
    private static final String BASE_DIR = "src/main/data/";

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        String fileName = name.replaceAll("\\.", "/");
        fileName = BASE_DIR + fileName + ".class";
        try {
            byte[] bytes = BinaryFileUtils.readFileToByteArray(fileName);
            return defineClass(name, bytes, 0, bytes.length);
        } catch (IOException e){
            throw new ClassNotFoundException("failed to load class " + name, e);
        }
    }
}
